<?php
$sub_menu = "230200";
include_once("_common.php");

auth_check($auth[$sub_menu], "r");

$token = get_token();

$qstr .= "&fr_date=$fr_date&to_date=$to_date&date=$date&rel_table=$rel_table&rel_id=$rel_id";

// 임시내역 삭제, 이틀 지난것 (PG)
$old_datetime = date("Y-m-d H:i:s", $g4[server_time] - 86400 * 30);
sql_query("delete from $mw_cash[cash_list_table] where cl_status = '' and cl_datetime < '$old_datetime'");

if (!is_numeric($page)) $page = 1;

$sql_common = " from $mw_cash[cash_list_table] as c, $g4[member_table] as m ";
$sql_search = " where cl_status = '1' and c.mb_id = m.mb_id ";

if ($stx) {
    $sql_search .= " and ( ";
    switch ($sfl) {
        case "mb_id" :
            $sql_search .= " (c.mb_id = '$stx') ";
            mw_cash_resum($stx);
            break;
        default : 
            $sql_search .= " ($sfl like '%$stx%') ";
            break;
    }
    $sql_search .= " ) ";
}

if ($date)
    $sql_search .= " and cl_datetime like '$date%' ";

if ($fr_date && $to_date)
    $sql_search .= " and cl_datetime between '$fr_date 00:00:00' and '$to_date 23:59:59' ";

if ($rel_table && $rel_id)
    $sql_search .= " and rel_table = '$rel_table' and rel_id = '$rel_id'  ";

if (!$sst) {
    $sst  = "cl_datetime";
    $sod = "desc";
}
$sql_order = " order by $sst $sod, cl_cash ";

$sql = "select count(*) as cnt
        $sql_common
        $sql_search";
$row = sql_fetch($sql);
$total_count = $row[cnt];

$rows = $config[cf_page_rows];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함  */

$sql = "select *
        $sql_common
        $sql_search
        $sql_order
        limit $from_record, $rows ";
$result = sql_query($sql);

$write_pages = get_paging($rows, $page, $total_page, "$PHP_SELF?{$qstr}&page=");


$listall = "<a href='$_SERVER[PHP_SELF]'>처음</a>";

if ($sfl == "mb_id" && $stx) {
    $mb = get_member($stx);
    $row = sql_fetch("select mb_cash from $mw_cash[cash_table] where mb_id = '$stx'");
    $mb[mb_cash] = $row[mb_cash];

    $sql_search = " cl_cash > 0 and cl_status = '1'  ";
    $sql_search.= " and cl_paytype <> '포인트' ";
    $sql_search.= " and cl_paytype <> '서비스' ";
    //$sql_search.= " and cl_paytype <> '페이팔' ";
    $sql_search.= " and cl_paytype <> '' ";

    $row = sql_fetch("select sum(cl_cash) as charge_cash from $mw_cash[cash_list_table] where mb_id = '$stx' and $sql_search");
    $mb[mb_charge_cash] = $row[charge_cash];
}

$g4[title] = "캐쉬사용내역";
include_once ("$g4[admin_path]/admin.head.php");

$colspan = 8;
?>

<script type="text/javascript" src="<?=$g4[path]?>/js/sideview.js"></script>
<script type="text/javascript">
var list_update_php = "";
var list_delete_php = "mw.cash.check.delete.php";
</script>

<table width=100%>
<form name=fsearch method=get>
<tr>
    <td width=50% align=left>
        <?=$listall?> (건수 : <?=number_format($total_count)?>)
        <? 
        if ($mb[mb_id]) {
            echo "&nbsp;(" . $mb[mb_id] ." 님 $mw_cash[cf_cash_name] 합계 : " . number_format($mb[mb_cash]) . "$mw_cash[cf_cash_unit]";
            echo ", 유료충전 : " . number_format($mb[mb_charge_cash]) . "$mw_cash[cf_cash_unit])";
        } else {
            $row2 = sql_fetch("select sum(mb_cash) as sum_cash from $mw_cash[cash_table]");
            echo "&nbsp;(전체 $mw_cash[cf_cash_name] 합계 : " . number_format($row2[sum_cash]) . "$mw_cash[cf_cash_unit])";
        }
        ?>
    </td>
    <td width=50% align=right>
        <select name=sfl class=cssfl>
            <option value='mb_id'>회원아이디</option>
            <option value='cl_content'>내용</option>
            <option value='cl_ip'>IP</option>
            <option value='cl_paylog'>결제로그</option>
            <option value='cl_admin_memo'>관리자메모</option>
            <? if ($mwp[admin]) { ?>
            <option value='mb_referer'>가입경로(파워팩)</option>
            <? } ?>
        </select>
        <input type=text name=stx class=ed required itemname='검색어' value='<?=$stx?>'>
        <input type=image src='<?=$g4[admin_path]?>/img/btn_search.gif' align=absmiddle></td>
</tr>
</form>
</table>

<form name=fpointlist method=post>
<input type=hidden name=sst   value='<?=$sst?>'>
<input type=hidden name=sod   value='<?=$sod?>'>
<input type=hidden name=sfl   value='<?=$sfl?>'>
<input type=hidden name=stx   value='<?=$stx?>'>
<input type=hidden name=page  value='<?=$page?>'>
<input type=hidden name=token value='<?=$token?>'>

<table width=100% cellpadding=0 cellspacing=1>
<colgroup width=30>
<colgroup width=80>
<colgroup width=90>
<colgroup width=140>
<colgroup width=''>
<colgroup width=70>
<colgroup width=60>
<tr><td colspan='<?=$colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td><input type=checkbox name=chkall value='1' onclick='check_all(this.form)'></td>
    <td><?=subject_sort_link('c.mb_id')?>회원아이디</a></td>
    <td>별명</td>
    <td><?=subject_sort_link('cl_datetime')?>일시</a></td>
    <td><?=subject_sort_link('cl_content')?><?=$mw_cash[cf_cash_name]?> 내용</a></td>
    <td><?=subject_sort_link('cl_cash')?><?=$mw_cash[cf_cash_name]?></a></td>
    <td><a href="mw.cash.form.php<? if ($sfl == 'mb_id') echo "?mb_id=$stx"; ?>"><img src="<?=$g4[admin_path]?>/img/icon_insert.gif"></a></td>
</tr>
<tr><td colspan='<?=$colspan?>' class='line2'></td></tr>
<?
for ($i=0; $row=sql_fetch_array($result); $i++) 
{
    if ($row2[mb_id] != $row[mb_id])
    {
        $sql2 = " select mb_id, mb_name, mb_nick, mb_email, mb_homepage, mb_point from $g4[member_table] where mb_id = '$row[mb_id]' ";
        $row2 = sql_fetch($sql2);
    }

    $mb_nick = get_sideview($row[mb_id], $row2[mb_nick], $row2[mb_email], $row2[mb_homepage]);

    $link1 = $link2 = "";
    if (!preg_match("/^\@/", $row[po_rel_table]) && $row[po_rel_table])
    {
        $link1 = "<a href='$g4[bbs_path]/board.php?bo_table={$row[po_rel_table]}&wr_id={$row[po_rel_id]}' target=_blank>";
        $link2 = "</a>";
    }

    $btn_mod = "<a href=\"mw.cash.form.php?w=u&cl_id=$row[cl_id]$qstr\"><img src=\"$g4[admin_path]/img/icon_modify.gif\"></a>";
    $btn_del = "<a href=\"javascript:del('mw.cash.update.php?w=d&cl_id=$row[cl_id]$qstr')\"><img src=\"$g4[admin_path]/img/icon_delete.gif\"></a>";

    $list = $i%2;
    echo "
    <input type=hidden name=cl_id[$i] value='$row[cl_id]'>
    <tr class='list$list col1 ht center'>
        <td><input type=checkbox name=chk[] value='$i'></td>
        <td><a href='?sfl=mb_id&stx=$row[mb_id]'>$row[mb_id]</a></td>
        <td>$mb_nick</td>
        <td>$row[cl_datetime]</td>
        <td align=left>&nbsp;{$link1}$row[cl_content]{$link2}</td>
        <td align=right>".number_format($row[cl_cash])." $mw_cash[cf_cash_unit]&nbsp;</td>
        <td> $btn_mod $btn_del </td>
    </tr> ";
} 

if ($i == 0)
    echo "<tr><td colspan='$colspan' align=center height=100 bgcolor=#ffffff>자료가 없습니다.</td></tr>";

echo "<tr><td colspan='$colspan' class='line2'></td></tr>";
echo "</table>";

$pagelist = get_paging($config[cf_write_pages], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");
echo "<table width=100% cellpadding=3 cellspacing=1>";
echo "<tr><td width=50%>";
echo "<input type=button class='btn1' value='선택삭제' onclick=\"btn_check(this.form, 'delete')\">";
echo "</td>";
echo "<td width=50% align=right>$pagelist</td></tr></table>\n";

if ($stx)
    echo "<script type='text/javascript'>document.fsearch.sfl.value = '$sfl';</script>\n";

if (strstr($sfl, "mb_id"))
    $mb_id = $stx;
else
    $mb_id = "";
?>
</form>

<script type="text/javascript"> document.fsearch.stx.focus(); </script>

<?
include_once ("$g4[admin_path]/admin.tail.php");
